/*
 * 2.3
 * Implement an algorithm to delete a node in the middle of a single linked list, given
 * only access to that node.
 * EXAMPLE
 * Input: the node ‘c’ from the linked list a->b->c->d->e
 * Result: nothing is returned, but the new linked list looks like a->b->d->e
 */
#include <stdio.h>
#include <stdlib.h>

typedef struct _node_t {
    int v;
    struct _node_t *next;
} node_t;

void delete_node(node_t *node)
{
    if (!node) {
        return;
    }
    node_t *curr = node, *next;
    while ((next = curr->next)) {
        curr->v = next->v;
        if (!next->next) {
            curr->next = NULL;
        }
        curr = next;
    }
}

node_t *new_node(int v)
{
    node_t *node = (node_t *)malloc(sizeof(node_t));
    node->v = v;
    return node;
}

int main()
{
    node_t *head = new_node(0);
    node_t *curr, *prev;
    int i = 1;
    prev = head;
    while (i < 7) {
        curr = new_node(i);
        prev->next = curr;
        prev = curr;
        i++;
    }
    delete_node(head->next->next->next);
    /* print & free */
    prev = head;
    while (prev) {
        curr = prev->next;
        printf("%d ", prev->v);
        free(prev);
        prev = curr;
    }
    printf("\n");
}

